package iut2.torque.generated;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.map.TableMap;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;





/**
 * Table Etudiants
 *
 * This class was autogenerated by Torque on:
 *
 * [Thu Jan 17 20:10:29 CET 2013]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Etudiant
 */
public abstract class BaseEtudiant extends BaseObject
{
    /** Serial version */
    private static final long serialVersionUID = 1358449829834L;

    /** The Peer class */
    private static final EtudiantPeer peer =
        new EtudiantPeer();


    /** The value for the etudiantId field */
    private int etudiantId;

    /** The value for the groupIdFk field */
    private int groupIdFk;

    /** The value for the firstName field */
    private String firstName;

    /** The value for the name field */
    private String name;

    /** The value for the absencesCount field */
    private int absencesCount;


    /**
     * Get the EtudiantId
     *
     * @return int
     */
    public int getEtudiantId()
    {
        return etudiantId;
    }


    /**
     * Set the value of EtudiantId
     *
     * @param v new value
     */
    public void setEtudiantId(int v) throws TorqueException
    {

        if (this.etudiantId != v)
        {
            this.etudiantId = v;
            setModified(true);
        }



        // update associated Note
        if (collNotes != null)
        {
            for (int i = 0; i < collNotes.size(); i++)
            {
                ((Note) collNotes.get(i))
                        .setEtudiantIdFk(v);
            }
        }
    }

    /**
     * Get the GroupIdFk
     *
     * @return int
     */
    public int getGroupIdFk()
    {
        return groupIdFk;
    }


    /**
     * Set the value of GroupIdFk
     *
     * @param v new value
     */
    public void setGroupIdFk(int v) throws TorqueException
    {

        if (this.groupIdFk != v)
        {
            this.groupIdFk = v;
            setModified(true);
        }


        if (aGroupe != null && !(aGroupe.getGroupId() == v))
        {
            aGroupe = null;
        }

    }

    /**
     * Get the FirstName
     *
     * @return String
     */
    public String getFirstName()
    {
        return firstName;
    }


    /**
     * Set the value of FirstName
     *
     * @param v new value
     */
    public void setFirstName(String v) 
    {

        if (!ObjectUtils.equals(this.firstName, v))
        {
            this.firstName = v;
            setModified(true);
        }


    }

    /**
     * Get the Name
     *
     * @return String
     */
    public String getName()
    {
        return name;
    }


    /**
     * Set the value of Name
     *
     * @param v new value
     */
    public void setName(String v) 
    {

        if (!ObjectUtils.equals(this.name, v))
        {
            this.name = v;
            setModified(true);
        }


    }

    /**
     * Get the AbsencesCount
     *
     * @return int
     */
    public int getAbsencesCount()
    {
        return absencesCount;
    }


    /**
     * Set the value of AbsencesCount
     *
     * @param v new value
     */
    public void setAbsencesCount(int v) 
    {

        if (this.absencesCount != v)
        {
            this.absencesCount = v;
            setModified(true);
        }


    }

    



    private Groupe aGroupe;

    /**
     * Declares an association between this object and a Groupe object
     *
     * @param v Groupe
     * @throws TorqueException
     */
    public void setGroupe(Groupe v) throws TorqueException
    {
        if (v == null)
        {
            setGroupIdFk( 0);
        }
        else
        {
            setGroupIdFk(v.getGroupId());
        }
        aGroupe = v;
    }


    /**
     * Returns the associated Groupe object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Groupe object
     * @throws TorqueException
     */
    public Groupe getGroupe()
        throws TorqueException
    {
        if (aGroupe == null && (this.groupIdFk != 0))
        {
            aGroupe = GroupePeer.retrieveByPK(SimpleKey.keyFor(this.groupIdFk));
        }
        return aGroupe;
    }

    /**
     * Return the associated Groupe object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Groupe object
     * @throws TorqueException
     */
    public Groupe getGroupe(Connection connection)
        throws TorqueException
    {
        if (aGroupe == null && (this.groupIdFk != 0))
        {
            aGroupe = GroupePeer.retrieveByPK(SimpleKey.keyFor(this.groupIdFk), connection);
        }
        return aGroupe;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setGroupeKey(ObjectKey key) throws TorqueException
    {

        setGroupIdFk(((NumberKey) key).intValue());
    }
   


    /**
     * Collection to store aggregation of collNotes
     */
    protected List collNotes;

    /**
     * Temporary storage of collNotes to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initNotes()
    {
        if (collNotes == null)
        {
            collNotes = new ArrayList();
        }
    }


    /**
     * Method called to associate a Note object to this object
     * through the Note foreign key attribute
     *
     * @param l Note
     * @throws TorqueException
     */
    public void addNote(Note l) throws TorqueException
    {
        getNotes().add(l);
        l.setEtudiant((Etudiant) this);
    }

    /**
     * Method called to associate a Note object to this object
     * through the Note foreign key attribute using connection.
     *
     * @param l Note
     * @throws TorqueException
     */
    public void addNote(Note l, Connection con) throws TorqueException
    {
        getNotes(con).add(l);
        l.setEtudiant((Etudiant) this);
    }

    /**
     * The criteria used to select the current contents of collNotes
     */
    private Criteria lastNotesCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getNotes(new Criteria())
     *
     * @return the collection of associated objects
     * @throws TorqueException
     */
    public List getNotes()
        throws TorqueException
    {
        if (collNotes == null)
        {
            collNotes = getNotes(new Criteria(10));
        }
        return collNotes;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Etudiant has previously
     * been saved, it will retrieve related Notes from storage.
     * If this Etudiant is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getNotes(Criteria criteria) throws TorqueException
    {
        if (collNotes == null)
        {
            if (isNew())
            {
               collNotes = new ArrayList();
            }
            else
            {
                criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId() );
                collNotes = NotePeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId());
                if (!lastNotesCriteria.equals(criteria))
                {
                    collNotes = NotePeer.doSelect(criteria);
                }
            }
        }
        lastNotesCriteria = criteria;

        return collNotes;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getNotes(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getNotes(Connection con) throws TorqueException
    {
        if (collNotes == null)
        {
            collNotes = getNotes(new Criteria(10), con);
        }
        return collNotes;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Etudiant has previously
     * been saved, it will retrieve related Notes from storage.
     * If this Etudiant is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getNotes(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collNotes == null)
        {
            if (isNew())
            {
               collNotes = new ArrayList();
            }
            else
            {
                 criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId());
                 collNotes = NotePeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                 criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId());
                 if (!lastNotesCriteria.equals(criteria))
                 {
                     collNotes = NotePeer.doSelect(criteria, con);
                 }
             }
         }
         lastNotesCriteria = criteria;

         return collNotes;
     }











    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Etudiant is new, it will return
     * an empty collection; or if this Etudiant has previously
     * been saved, it will retrieve related Notes from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Etudiant.
     */
    protected List getNotesJoinEtudiant(Criteria criteria)
        throws TorqueException
    {
        if (collNotes == null)
        {
            if (isNew())
            {
               collNotes = new ArrayList();
            }
            else
            {
                criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId());
                collNotes = NotePeer.doSelectJoinEtudiant(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            criteria.add(NotePeer.ETUDIANT_ID_FK, getEtudiantId());
            if (!lastNotesCriteria.equals(criteria))
            {
                collNotes = NotePeer.doSelectJoinEtudiant(criteria);
            }
        }
        lastNotesCriteria = criteria;

        return collNotes;
    }



        
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
            fieldNames.add("EtudiantId");
            fieldNames.add("GroupIdFk");
            fieldNames.add("FirstName");
            fieldNames.add("Name");
            fieldNames.add("AbsencesCount");
            fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by field (Java) name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
        if (name.equals("EtudiantId"))
        {
            return new Integer(getEtudiantId());
        }
        if (name.equals("GroupIdFk"))
        {
            return new Integer(getGroupIdFk());
        }
        if (name.equals("FirstName"))
        {
            return getFirstName();
        }
        if (name.equals("Name"))
        {
            return getName();
        }
        if (name.equals("AbsencesCount"))
        {
            return new Integer(getAbsencesCount());
        }
        return null;
    }

    /**
     * Set a field in the object by field (Java) name.
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByName(String name, Object value )
        throws TorqueException, IllegalArgumentException
    {
        if (name.equals("EtudiantId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setEtudiantId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("GroupIdFk"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setGroupIdFk(((Integer) value).intValue());
            return true;
        }
        if (name.equals("FirstName"))
        {
            // Object fields can be null
            if (value != null && ! String.class.isInstance(value))
            {
                throw new IllegalArgumentException("Invalid type of object specified for value in setByName");
            }
            setFirstName((String) value);
            return true;
        }
        if (name.equals("Name"))
        {
            // Object fields can be null
            if (value != null && ! String.class.isInstance(value))
            {
                throw new IllegalArgumentException("Invalid type of object specified for value in setByName");
            }
            setName((String) value);
            return true;
        }
        if (name.equals("AbsencesCount"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setAbsencesCount(((Integer) value).intValue());
            return true;
        }
        return false;
    }

    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
        if (name.equals(EtudiantPeer.ETUDIANT_ID))
        {
            return new Integer(getEtudiantId());
        }
        if (name.equals(EtudiantPeer.GROUP_ID_FK))
        {
            return new Integer(getGroupIdFk());
        }
        if (name.equals(EtudiantPeer.FIRST_NAME))
        {
            return getFirstName();
        }
        if (name.equals(EtudiantPeer.NAME))
        {
            return getName();
        }
        if (name.equals(EtudiantPeer.ABSENCES_COUNT))
        {
            return new Integer(getAbsencesCount());
        }
        return null;
    }

    /**
     * Set field values by Peer Field Name
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPeerName(String name, Object value)
        throws TorqueException, IllegalArgumentException
    {
      if (EtudiantPeer.ETUDIANT_ID.equals(name))
        {
            return setByName("EtudiantId", value);
        }
      if (EtudiantPeer.GROUP_ID_FK.equals(name))
        {
            return setByName("GroupIdFk", value);
        }
      if (EtudiantPeer.FIRST_NAME.equals(name))
        {
            return setByName("FirstName", value);
        }
      if (EtudiantPeer.NAME.equals(name))
        {
            return setByName("Name", value);
        }
      if (EtudiantPeer.ABSENCES_COUNT.equals(name))
        {
            return setByName("AbsencesCount", value);
        }
        return false;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
        if (pos == 0)
        {
            return new Integer(getEtudiantId());
        }
        if (pos == 1)
        {
            return new Integer(getGroupIdFk());
        }
        if (pos == 2)
        {
            return getFirstName();
        }
        if (pos == 3)
        {
            return getName();
        }
        if (pos == 4)
        {
            return new Integer(getAbsencesCount());
        }
        return null;
    }

    /**
     * Set field values by its position (zero based) in the XML schema.
     *
     * @param position The field position
     * @param value field value
     * @return True if value was set, false if not (invalid position / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPosition(int position, Object value)
        throws TorqueException, IllegalArgumentException
    {
    if (position == 0)
        {
            return setByName("EtudiantId", value);
        }
    if (position == 1)
        {
            return setByName("GroupIdFk", value);
        }
    if (position == 2)
        {
            return setByName("FirstName", value);
        }
    if (position == 3)
        {
            return setByName("Name", value);
        }
    if (position == 4)
        {
            return setByName("AbsencesCount", value);
        }
        return false;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
        save(EtudiantPeer.DATABASE_NAME);
    }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
     *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
        try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
    }

    /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
        if (!alreadyInSave)
        {
            alreadyInSave = true;



            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    EtudiantPeer.doInsert((Etudiant) this, con);
                    setNew(false);
                }
                else
                {
                    EtudiantPeer.doUpdate((Etudiant) this, con);
                }
            }


            if (collNotes != null)
            {
                for (int i = 0; i < collNotes.size(); i++)
                {
                    ((Note) collNotes.get(i)).save(con);
                }
            }
            alreadyInSave = false;
        }
    }


    /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param key etudiantId ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        throws TorqueException
    {
        setEtudiantId(((NumberKey) key).intValue());
    }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
        setEtudiantId(Integer.parseInt(key));
    }


    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
        return SimpleKey.keyFor(getEtudiantId());
    }
 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     */
    public Etudiant copy() throws TorqueException
    {
        return copy(true);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     *
     * @param con the database connection to read associated objects.
     */
    public Etudiant copy(Connection con) throws TorqueException
    {
        return copy(true, con);
    }

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     */
    public Etudiant copy(boolean deepcopy) throws TorqueException
    {
        return copyInto(new Etudiant(), deepcopy);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     * @param con the database connection to read associated objects.
     */
    public Etudiant copy(boolean deepcopy, Connection con) throws TorqueException
    {
        return copyInto(new Etudiant(), deepcopy, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     */
    protected Etudiant copyInto(Etudiant copyObj) throws TorqueException
    {
        return copyInto(copyObj, true);
    }

  
    /**
     * Fills the copyObj with the contents of this object using connection.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param con the database connection to read associated objects.
     */
    protected Etudiant copyInto(Etudiant copyObj, Connection con) throws TorqueException
    {
        return copyInto(copyObj, true, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     */
    protected Etudiant copyInto(Etudiant copyObj, boolean deepcopy) throws TorqueException
    {
        copyObj.setEtudiantId(etudiantId);
        copyObj.setGroupIdFk(groupIdFk);
        copyObj.setFirstName(firstName);
        copyObj.setName(name);
        copyObj.setAbsencesCount(absencesCount);

        copyObj.setEtudiantId( 0);

        if (deepcopy)
        {


        List vNotes = getNotes();
        if (vNotes != null)
        {
            for (int i = 0; i < vNotes.size(); i++)
            {
                Note obj = (Note) vNotes.get(i);
                copyObj.addNote(obj.copy());
            }
        }
        else
        {
            copyObj.collNotes = null;
        }
        }
        return copyObj;
    }
        
    
    /**
     * Fills the copyObj with the contents of this object using connection.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     * @param con the database connection to read associated objects.
     */
    protected Etudiant copyInto(Etudiant copyObj, boolean deepcopy, Connection con) throws TorqueException
    {
        copyObj.setEtudiantId(etudiantId);
        copyObj.setGroupIdFk(groupIdFk);
        copyObj.setFirstName(firstName);
        copyObj.setName(name);
        copyObj.setAbsencesCount(absencesCount);

        copyObj.setEtudiantId( 0);

        if (deepcopy)
        {


        List vNotes = getNotes(con);
        if (vNotes != null)
        {
            for (int i = 0; i < vNotes.size(); i++)
            {
                Note obj = (Note) vNotes.get(i);
                copyObj.addNote(obj.copy(con), con);
            }
        }
        else
        {
            copyObj.collNotes = null;
        }
        }
        return copyObj;
    }
    
    

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public EtudiantPeer getPeer()
    {
        return peer;
    }

    /**
     * Retrieves the TableMap object related to this Table data without
     * compiler warnings of using getPeer().getTableMap().
     *
     * @return The associated TableMap object.
     */
    public TableMap getTableMap() throws TorqueException
    {
        return EtudiantPeer.getTableMap();
    }


    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Etudiant:\n");
        str.append("EtudiantId = ")
           .append(getEtudiantId())
           .append("\n");
        str.append("GroupIdFk = ")
           .append(getGroupIdFk())
           .append("\n");
        str.append("FirstName = ")
           .append(getFirstName())
           .append("\n");
        str.append("Name = ")
           .append(getName())
           .append("\n");
        str.append("AbsencesCount = ")
           .append(getAbsencesCount())
           .append("\n");
        return(str.toString());
    }
}
